﻿Public Class MainWindow
    Private Shared ReadOnly IsLongProcessRunningPropertyKey As DependencyPropertyKey =
        DependencyProperty.RegisterReadOnly(
            name:="IsLongProcessRunning",
            propertyType:=GetType(Boolean),
            ownerType:=GetType(MainWindow),
            typeMetadata:=New PropertyMetadata(),
            validateValueCallback:=Nothing)
    Public Shared ReadOnly IsLongProcessRunningProperty As DependencyProperty =
        IsLongProcessRunningPropertyKey.DependencyProperty
    Public Property IsLongProcessRunning As Boolean
        Get
            Return Me.GetValue(IsLongProcessRunningProperty)
        End Get
        Private Set(value As Boolean)
            If (Me.IsLongProcessRunning <> value) Then
                Me.SetValue(IsLongProcessRunningPropertyKey, value)
            End If
        End Set
    End Property

    Private Shared ReadOnly LongProcessProgressPropertyKey As DependencyPropertyKey =
        DependencyProperty.RegisterReadOnly(
            name:="LongProcessProgress",
            propertyType:=GetType(Integer),
            ownerType:=GetType(MainWindow),
            typeMetadata:=New PropertyMetadata(),
            validateValueCallback:=Nothing
        )
    Public Shared ReadOnly LongProcessProgressProperty As DependencyProperty =
        LongProcessProgressPropertyKey.DependencyProperty
    Public Property LongProcessProgress As Integer
        Get
            Return Me.GetValue(LongProcessProgressProperty)
        End Get
        Set(value As Integer)
            If (Me.LongProcessProgress <> value) Then
                Me.SetValue(LongProcessProgressPropertyKey, value)
            End If
        End Set
    End Property

    Private Shared ReadOnly LongProcessUserStatePropertyKey As DependencyPropertyKey =
        DependencyProperty.RegisterReadOnly(
            name:="LongProcessUserState",
            propertyType:=GetType(Object),
            ownerType:=GetType(MainWindow),
            typeMetadata:=New PropertyMetadata(),
            validateValueCallback:=Nothing
        )
    Public Shared ReadOnly LongProcessUserStateProperty As DependencyProperty =
        LongProcessUserStatePropertyKey.DependencyProperty
    Public Property LongProcessUserState As Object
        Get
            Return Me.GetValue(LongProcessUserStateProperty)
        End Get
        Set(value As Object)
            If (Me.LongProcessUserState IsNot value) Then
                Me.SetValue(LongProcessUserStatePropertyKey, value)
            End If
        End Set
    End Property

    Private _viewModel As New ViewModel.MainViewModel(ViewNavigator.Instance)
    Public ReadOnly Property ViewModel As ViewModel.MainViewModel
        Get
            Return _viewModel
        End Get
    End Property

    Private Sub MainWindow_Initialized(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Initialized
        AddHandler ViewNavigator.Instance.LongProcessStarted, AddressOf ViewNavigator_LongProcessStarted
        AddHandler ViewNavigator.Instance.LongProcessProgressUpdated, AddressOf ViewNavigator_LongProcessProgressUpdated
        AddHandler ViewNavigator.Instance.LongProcessCompleted, AddressOf ViewNavigator_LongProcessCompleted
    End Sub

    Private Sub ViewNavigator_LongProcessStarted(sender As Object, e As EventArgs)
        Me.IsLongProcessRunning = True
    End Sub

    Private Sub ViewNavigator_LongProcessCompleted(sender As Object, e As EventArgs)
        Me.IsLongProcessRunning = False
    End Sub

    Private Sub ViewNavigator_LongProcessProgressUpdated(sender As Object, progressPercentage As Integer, userState As Object)
        Me.LongProcessProgress = progressPercentage
        Me.LongProcessUserState = userState
    End Sub

End Class
